package com.cci.kangdao.utilTool;

import com.cci.kangdao.config.EmailConfiguration;
import com.cci.kangdao.context.ApiApplicationContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * 邮件工具类
 * @author Eric
 *
 */
public class MailUtil {

	private static String starttlsEnable;

	private static String enablePort = "false";

    private static Logger log = Logger.getLogger(MailUtil.class);
	public static void set(String starttlsEnable, String enablePort) {
		MailUtil.starttlsEnable = starttlsEnable;
		MailUtil.enablePort = enablePort;
	}



	/**
	 * 创建邮件发送器
	 * @return
	 */
	private static JavaMailSenderImpl createMailSender() {

		EmailConfiguration emailConfiguration = ApiApplicationContext.getBean(EmailConfiguration.class);

        String host = emailConfiguration.getHost();
        String port = emailConfiguration.getPort();
        String username = emailConfiguration.getUsername();
        String password = emailConfiguration.getPassword();
        String timeout = emailConfiguration.getTimeout();

        if(StringUtils.isEmpty(starttlsEnable)) {
        	starttlsEnable = emailConfiguration.getStarttlsEnable();
        }

        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(host);
        sender.setPort(Integer.parseInt(port));
        sender.setUsername(username);
        sender.setPassword(password);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
		p.put("mail.smtp.connectiontimeout", timeout);// 设置接收超时时间
		p.put("mail.smtp.writetimeout", timeout);
        p.setProperty("mail.smtp.timeout", timeout);
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.starttls.enable", starttlsEnable);
		p.setProperty("mail.smtp.ssl.protocols", "TLSv1.2");
        if(enablePort.matches("^(true)$")) {
        	p.put("mail.smtp.ssl.trust", port);
        }

        sender.setJavaMailProperties(p);
        return sender;
    }

	/**
	 * 发送邮件
	 * @param to 收件人
	 * @param subject 邮件主题
	 * @param content 邮件内容
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException
	 */
	public static void sendMail(String to, String subject, String content) throws MessagingException,UnsupportedEncodingException {

		try {
			EmailConfiguration emailConfiguration = ApiApplicationContext.getBean(EmailConfiguration.class);
			String from = emailConfiguration.getFrom();
			/**
			 * sxt
			 * Privacy Violation
			 * 20230816
			 */
			//log.error(emailConfiguration.toString());
			JavaMailSenderImpl mailSender = createMailSender();
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			// 设置utf-8或GBK编码，否则邮件会有乱码
			MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
			messageHelper.setFrom(from);
			messageHelper.setTo(to);
			messageHelper.setSubject(subject);
			messageHelper.setText(content, true);
			mailSender.send(mimeMessage);
		} catch (MailException e) {
			e.printStackTrace();
		}
    }


}
